<template>
  <div class="acitivbox" :class="{owidth:pushStatus}">
    <div v-if="!pushStatus">
    <el-row style="margin-bottom:10px">
      <el-col :span="8" >
        <h4 style="margin:0">
          <el-button  type="success" class="addBtn" @click="addUserFun">添加活动</el-button>          
        </h4>
      </el-col>
      <el-col :span="16" style="text-align:right">
        
      </el-col>
    </el-row>
   
    <el-table :data="tableData" border style="width: 100%" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55"></el-table-column>-->
      <el-table-column prop="ID" label="序号" width="60">
         <template slot-scope="scope">{{scope.$index+1}}</template>
      </el-table-column>
      <el-table-column prop="name" label="活动标题"></el-table-column>
      <el-table-column prop="begin_time" label="开始时间" width="180">
        <template slot-scope="scope">
          {{timeFun(scope.row.begin_time)}}
        </template>
      </el-table-column>
      <el-table-column prop="end_time" label="结束时间" width="180">
        <template slot-scope="scope">
          {{timeFun(scope.row.end_time)}}
        </template>
      </el-table-column>      
      <el-table-column prop="page_view" label="浏览量"></el-table-column>
      <el-table-column prop="player_count" label="参与量"></el-table-column>
      <el-table-column prop="status" label="审核状态">
         <template slot-scope="scope">
          <span v-if="scope.row.status == 0">审核中</span>
          <span v-if="scope.row.status == 1">审核通过</span>
          <span v-if="scope.row.status == 2" style="color:red">审核不通过</span>
        </template>
      </el-table-column>
      <el-table-column prop="tui" label="活动状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">{{activTime(scope.row)}}</span>
          <span v-else>待审核</span>
        </template>
      </el-table-column>
      <el-table-column width="150" label="操作">
        <template slot-scope="scope">
          <div>
            <el-button class="addBtn" type="primary" @click="infoFun(scope.row)">编辑</el-button>
            <el-button class="addBtn" type="danger" @click="deleteFun(scope.row.id)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage4"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
    </div>
    </div>
     <div v-if="pushStatus">
       <Addactivity  @getlist="getlist" ref="artice" :editData="editData"></Addactivity>
    </div>
    
  </div>
</template>

<script>
  import Addactivity from './Activitem/Addactivity'
  import Http from '@/Http/Http'
  import getTime from '@/Http/getTime'
  export default {
    data() {
      return {
        pushStatus:false,
        value:'关注',
        editData:'',
        total:0,
        currentPage4: 1,//分野  
        page:1,
        pageSize:10,
        tableData: []
      }
    },
    components:{
      Addactivity
    },
    created(){
      this.getlist()
    },
    methods: {
      getlist(){
        this.editData='';
        Http.getActivitysList({
          "pageIndex":this.page,
          "pageSize":this.pageSize
        }).then(res => {
          this.tableData = res.data.data.rows;
          this.total = res.data.data.total
          console.log(res)
        })
        this.pushStatus = false
      },
       getdata (evt) {
            console.log(evt.draggedContext.element.text)
        },
        datadragEnd (evt) {
            evt.preventDefault();
            console.log('拖动前的索引 :' + evt.oldIndex)
            console.log('拖动后的索引 :' + evt.newIndex)
            console.log(this.colors);
        },
      activTime(row){
        const val = Math.round(new Date());
        if(val < row.begin_time) {
            return '活动未开始'
        } else if(row.begin_time < val && val < row.end_time) {  
          return ' 活动中'
        } else {
          return '活动已结束'
        }
      },
      timeFun(val) {
        return getTime(val)
      },
      deleteFun (row){
        this.$confirm(`确认删除此活动吗？`)
        .then(_ => {
          Http.deletActivity(row)
          .then( res => {
            if( res.data.code == 200 ){
              this.$message.success('删除成功')
              this.getlist()         
            } else if(res.data.code == -1003) {
              this.$message.error(`请求失败原因：${res.data.message}，请重新登录`)
            }
          })
          .catch( error => {
            this.$message.error('服务器请求失败')
          })
        })
        .catch(_ => {
          //取消部分
          console.log('bb')
        });
       
      },
      infoFun (row){
        console.log(row)
        this.editData = row;
        this.pushStatus = true
      },
      //添加用户
      addUserFun() {
        this.pushStatus = true
      },
      
      //选择 推荐
      handleSelectionChange() {
        
      },

          //分页
      handleSizeChange(val) {
        this.pageSize = val;
        this.getlist()
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        this.page = val;
        this.getlist()
        console.log(`当前页: ${val}`);
      },
    }
  }
</script>
<style  scoped>
.owidth{
  width: 80%
}
.acitivbox{
  background: #fff;
  padding:10px;
  min-height: 500px
}
</style>
